package cc.flybb.roombasic_step;

import android.app.Application;
import android.content.Context;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import java.util.List;

public class WordRepository {

    private WordDao wordDao;
    private LiveData<List<Word>> allWordsLive;

//    WordRepository(Context context) {
//        WordDatabase wordDatabase = WordDatabase.getDatabase(context.getApplicationContext());
//        wordDao = wordDatabase.getWordDao();
//        allWordsLive = wordDao.getAllWordsLive();
//    }



    // Note that in order to unit test the WordRepository, you have to remove the Application
    // dependency. This adds complexity and much more code, and this sample is not about testing.
    // See the BasicSample in the android-architecture-components repository at
    // https://github.com/googlesamples
    WordRepository(Application application) {
        WordDatabase wordDatabase = WordDatabase.getDatabase(application);
        wordDao = wordDatabase.getWordDao();
        allWordsLive = wordDao.getAllWordsLive();
    }

    // Room executes all queries on a separate thread.
    // Observed LiveData will notify the observer when the data has changed.
    LiveData<List<Word>> getAllWords() {
        return allWordsLive;
    }

    // You must call this on a non-UI thread or your app will throw an exception. Room ensures
    // that you're not doing any long running operations on the main thread, blocking the UI.
    void insertWords(final Word... words) {
        WordDatabase.databaseWriteExecutor.execute(new Runnable() {
            @Override
            public void run() {
                wordDao.insertWords(words);
            }
        });
    }

    void updateWords(final Word... words) {
        WordDatabase.databaseWriteExecutor.execute(new Runnable() {
            @Override
            public void run() {
                wordDao.updateWords(words);
            }
        });
    }

    void deleteWords(final Word... words) {
        WordDatabase.databaseWriteExecutor.execute(new Runnable() {
            @Override
            public void run() {
                wordDao.deleteWords(words);
            }
        });
    }

    void deleteAllWords() {
        WordDatabase.databaseWriteExecutor.execute(new Runnable() {
            @Override
            public void run() {
                wordDao.deleteAllWords();
            }
        });
    }





//    void insertWords(Word... words) {
//        new InsertAsyncTask(wordDao).execute(words);
//    }
//
//    void updateWords(Word... words) {
//        new UpdateAsyncTask(wordDao).execute(words);
//    }
//
//    void deleteWords(Word... words) {
//        new DeleteAsyncTask(wordDao).execute(words);
//    }
//
//    void deleteAllWords(Word... words) {
//        new DeleteAllAsyncTask(wordDao).execute();
//    }
//
//    static class InsertAsyncTask extends AsyncTask<Word, Void, Void> {
//        private WordDao wordDao;
//
//        InsertAsyncTask(WordDao wordDao) {
//            this.wordDao = wordDao;
//        }
//
//        @Override
//        protected Void doInBackground(Word... words) {
//            wordDao.insertWords(words);
//            return null;
//        }
//
//    }
//
//    static class UpdateAsyncTask extends AsyncTask<Word, Void, Void> {
//        private WordDao wordDao;
//
//        UpdateAsyncTask(WordDao wordDao) {
//            this.wordDao = wordDao;
//        }
//
//        @Override
//        protected Void doInBackground(Word... words) {
//            wordDao.updateWords(words);
//            return null;
//        }
//
//    }
//
//    static class DeleteAsyncTask extends AsyncTask<Word, Void, Void> {
//        private WordDao wordDao;
//
//        DeleteAsyncTask(WordDao wordDao) {
//            this.wordDao = wordDao;
//        }
//
//        @Override
//        protected Void doInBackground(Word... words) {
//            wordDao.deleteWords(words);
//            return null;
//        }
//
//    }
//
//    static class DeleteAllAsyncTask extends AsyncTask<Void, Void, Void> {
//        private WordDao wordDao;
//
//        DeleteAllAsyncTask(WordDao wordDao) {
//            this.wordDao = wordDao;
//        }
//
//        @Override
//        protected Void doInBackground(Void... voids) {
//            wordDao.deleteAllWords();
//            return null;
//        }
//
//    }
}
